package cli

import (
	"errors"
	"fmt"
	"gitee.com/zhangyu520cw/cloud-station/store"
	"gitee.com/zhangyu520cw/cloud-station/store/aliyun"
	"github.com/spf13/cobra"
)

var (
	ossProvider  string
	ossEndpoint  string
	ossAccessKey string
	ossSecretkey string
	bucketname   string
	filename     string
	destname     string
)
var UploadCmd = &cobra.Command{
	Use:     "upload",
	Long:    "upload 文件上传",
	Short:   "upload 文件上传",
	Example: "upload -f filename",
	RunE: func(cmd *cobra.Command, args []string) error {
		var (
			uploader store.Uploader
			err      error
		)
		switch ossProvider {
		case "aliyun":
			uploader, err = aliyun.NewAliOssStore(&aliyun.Options{
				EndPoint:     ossEndpoint,
				AccessKey:    ossAccessKey,
				AccessSecret: ossSecretkey,
			})
		case "tx":
			fmt.Println("后期加入")
		case "mino":
			fmt.Println("后期加入")
		default:
			return errors.New("not support oss storage provider")
		}
		if err != nil {
			return err
		}
		uploader.Upload(bucketname, destname, filename)
		return nil
	},
}

func init() {
	f := UploadCmd.PersistentFlags()
	f.StringVarP(&ossProvider, "provider", "p",
		"aliyun", "oss storage provider[aliyun/tx/mino]")
	f.StringVarP(&ossEndpoint, "endpoint", "e",
		"oss-cn-beijing.aliyuncs.com", "oss storage provider endpoint[域名]")
	f.StringVarP(&bucketname, "bucketname", "b", "devcloud-station25", "oss storage provider bucketname")
	f.StringVarP(&ossAccessKey, "accesskey", "k", "", "oss storage provider accesskey")
	f.StringVarP(&ossSecretkey, "secretkey", "s", "", "oss storage provider Secretkey")
	f.StringVarP(&filename, "filename", "f", "", "upload filename")
	f.StringVarP(&destname, "destname", "d", filename, "upload destname")
	RootCmd.AddCommand(UploadCmd)
}
